package com.hq.ims.platform.busservice;

import com.hq.authority.utils.AppUtils;
import com.hq.ims.data.entity.*;
import com.hq.ims.data.service.*;
import com.hq.ims.data.utils.ErrMsgConstant;
import com.hq.ims.data.vo.UserMapstruct;
import com.hq.utils.util.ListUtils;
import com.hq.utils.util.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统管理中心Controller
 */
@Service
public class PlatformMyInfoService {

    @Resource
    private BaseUserService baseUserService;
    @Resource
    private BaseCompanyService baseCompanyService;
    @Resource
    private BasePermissionMenuService basePermissionMenuService;
    @Resource
    private BaseAllowLoginPlatformCompanyService baseAllowLoginPlatformCompanyService;
    @Resource
    private BaseCompanyEmployeeService baseCompanyEmployeeService;
    @Resource
    private BaseCompanyDepartmentEmployeeService baseCompanyDepartmentEmployeeService;
    @Resource
    private BaseCompanyDepartmentService baseCompanyDepartmentService;


    /**
     * 获取当前tokenUser信息
     */
    public UserMapstruct.UserAndCompanySimpleVo getMyInfo(
    ) {
        String userId = AppUtils.getUserId();
        User curUserInfo = baseUserService.getById(userId);
        ObjectUtils.isNullThrow(curUserInfo, ErrMsgConstant.USER_INFO_NOT_EXISTS);

        //允许登陆平台的公司
        List<AllowLoginPlatformCompany> allowLoginPlatformCompanyList = baseAllowLoginPlatformCompanyService.lambdaQuery()
                .select(AllowLoginPlatformCompany::getCompanyId)
                .list();
        ListUtils.isNotExistsEleTh(allowLoginPlatformCompanyList, ErrMsgConstant.ACCOUNT_ILLEGALITY_LOGIN);

        //当前用户是否工司员工
        List<String> allowLogiCompanyIdList = allowLoginPlatformCompanyList.stream().map(allowLoginPlatformCompany -> allowLoginPlatformCompany.getCompanyId()).collect(Collectors.toList());
        List<CompanyEmployee> companyEmployeeList = baseCompanyEmployeeService.lambdaQuery()
                .eq(CompanyEmployee::getUserId, userId)
                .in(CompanyEmployee::getCompanyId, allowLogiCompanyIdList)
                .list();
        ListUtils.isNotExistsEleTh(allowLoginPlatformCompanyList, ErrMsgConstant.USER_UN_JOIN_COMPANY);

        //公司员工
        CompanyEmployee companyEmployee = companyEmployeeList.stream().filter(d -> allowLogiCompanyIdList.contains(d.getCompanyId())).collect(Collectors.toList()).stream().findFirst().get();
        ObjectUtils.isNullThrow(companyEmployee, ErrMsgConstant.USER_UN_JOIN_COMPANY);

        //查找用户公司
        Company company = baseCompanyService.getById(companyEmployee.getCompanyId());
        ObjectUtils.isNullThrow(companyEmployee, ErrMsgConstant.COMPANY_INFO_NOT_EXISTS);

        //先组装简单对象
        UserMapstruct.UserAndCompanySimpleVo curUerSimpleInfo = new UserMapstruct.UserAndCompanySimpleVo();
        curUerSimpleInfo.setUserId(userId);
        curUerSimpleInfo.setRealName(curUserInfo.getRealName());
        curUerSimpleInfo.setAvatarUrl(curUserInfo.getAvatarUrl());
        curUerSimpleInfo.setAccountStatus(curUserInfo.getAccountStatus());
        curUerSimpleInfo.setSex(curUserInfo.getSex());
        curUerSimpleInfo.setMobile(curUserInfo.getMobile());
        curUerSimpleInfo.setRegisterTime(curUserInfo.getCreateDateTime());

        curUerSimpleInfo.setCompanyId(company.getCompanyId());
        curUerSimpleInfo.setCompanyName(company.getCompanyName());
        curUerSimpleInfo.setCompanyLevel(company.getCompanyLevel());

        //查找用户部门,可能不存在部门
        List<CompanyDepartmentEmployee> companyDepartmentEmployeeList = baseCompanyDepartmentEmployeeService.getByUserIdAndDepartmentId(userId, company.getCompanyId());
        if (ListUtils.isExistsEle(companyDepartmentEmployeeList)) {
            ListUtils.sortInt(companyDepartmentEmployeeList, d -> d.getIsPartTimeJob() ? 1 : 0, true);
            CompanyDepartmentEmployee companyDepartmentEmployee = companyDepartmentEmployeeList.get(0);
            CompanyDepartment companyDepartment = baseCompanyDepartmentService.getById(companyDepartmentEmployee.getCompanyDepartmentId());
            curUerSimpleInfo.setCompanyDepartmentId(companyDepartment.getCompanyDepartmentId());
            curUerSimpleInfo.setCompanyDepartmentName(companyDepartment.getCompanyDepartmentName());
            curUerSimpleInfo.setIsCompanyDepartmentPrincipal(companyDepartmentEmployee.getIsPrincipal());
            curUerSimpleInfo.setJobName(companyDepartmentEmployee.getJobName());
            curUerSimpleInfo.setIsCompanyAdmin(companyEmployee.getIsAdmin());
        }
        return curUerSimpleInfo;
    }

}
